package com.psa.system;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.*;
import com.psa.adapter.SqlObjectListAdapter;
import com.psa.element.*;
import com.psa.tool.*;
import com.wxw.BaseLibrary.tool.ActivityTool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 2014/5/13.
 */
public class CustomerSearchActivity extends Activity implements View.OnClickListener,IGeneralEventCallback {
    private ListView customer_list;
    private TextView tv_info;
    private Button search_customer,search_customer_reset,back_button;
    private EditText et_customer_name,et_customer_address,et_customer_phone;
    private List<ISqlObject> customers = new ArrayList<ISqlObject>();

    private String whereName,whereAdress,wherePhone;

    private Runnable showRequestChangedCallback = new Runnable() {
        @Override
        public void run() {
            tv_info.setText(SystemStatus.getReqCount());
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTool.hideTitle(this);
        ActivityTool.fullScreen(this);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.searchcustomer_view);
        customer_list = (ListView)findViewById(R.id.lv_customers);
        customer_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ((SqlObjectListAdapter)customer_list.getAdapter()).setIndex(i);
                SystemStatus.currentCustomer = (Customer)customers.get(i);
                RequestManager.getRequestManager().removeGeneralEventListener(CustomerSearchActivity.this);
//                CustomerSearchActivity.this.startActivityForResult(new Intent(CustomerSearchActivity.this, CustomerActivity.class), 0);
                setResult(1);
                finish();
            }
        });
        tv_info = (TextView)findViewById(R.id.tv_info);
        search_customer = (Button)findViewById(R.id.search_customer);
        search_customer.setOnClickListener(this);
        search_customer_reset = (Button)findViewById(R.id.search_customer_reset);
        search_customer_reset.setOnClickListener(this);
        back_button = (Button)findViewById(R.id.back_button);
        back_button.setOnClickListener(this);
        et_customer_name = (EditText)findViewById(R.id.et_customer_name);
//        et_customer_address = (EditText)findViewById(R.id.et_customer_address);
//        et_customer_phone = (EditText)findViewById(R.id.et_customer_phone);

        SqlObjectListAdapter adapter = new SqlObjectListAdapter(this, customers);
        customer_list.setAdapter(adapter);
        RequestManager.getRequestManager().addGeneralEventListener(this);
        tv_info.setText(SystemStatus.getReqCount());
    }

    @Override
    public void finished(Object sender, String eventContent) {
        if (sender instanceof RequestManager){
//            SystemStatus.ReqCount = eventContent;
            CustomerSearchActivity.this.runOnUiThread(showRequestChangedCallback);
        } else {

        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        RequestManager.getRequestManager().addGeneralEventListener(this);
//        switch (requestCode){
//            case 0:
//                break;
//            default:
//                break;
//        }
//        super.onActivityResult(requestCode, resultCode, data);
//    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.search_customer) {
            if (et_customer_phone.getText().toString().trim().equals("")) {
                wherePhone="'%'";
            } else {
                wherePhone="'%" + et_customer_phone.getText().toString().trim()+"%' ";
            }

            if (et_customer_name.getText().toString().trim().equals("")) {
                whereName="'%'";
            } else {
                whereName="'%" + et_customer_name.getText().toString().trim()+"%' ";
            }

            if (et_customer_address.getText().toString().trim().equals("")) {
                whereAdress="'%'";
            } else {
                whereAdress="'%" + et_customer_address.getText().toString().trim()+"%' ";
            }

            refrashCustomers();
        } else if (view.getId() == R.id.search_customer_reset) {
            et_customer_phone.setText("");
            et_customer_name.setText("");
            et_customer_address.setText("");
            et_customer_phone.requestFocus();
        } else {
            RequestManager.getRequestManager().removeGeneralEventListener(this);
            setResult(0);
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            RequestManager.getRequestManager().removeGeneralEventListener(this);
        }
        return super.onKeyDown(keyCode, event);
    }

    private void refrashCustomers(){
        if (whereAdress.equals("") && whereName.equals("") && wherePhone.equals("")){
            SystemStatus.showMessage(this, getString(R.string.customer_manager_code_search_errmsg));
            return;
        }
        customers.clear();
        String where = "";
//        if (!whereCode.equals(""))
//            where = whereCode;
//        else if (!wherePhone.equals(""))
//            where = wherePhone;
//        else
//            where = whereName;
        Cursor cursor = DBTool.getInstance().query("Select * From Customer Where Telephone Like "+ wherePhone
                + " And ChineseName Like " + whereName + " And ChineseAddress Like " + whereAdress);
        Customer customer = new Customer();
        while ((customer = (Customer)customer.full(cursor)) != null) {
            customers.add(customer);
        }
        if (cursor!=null) {
            cursor.close();
        }
        ((SqlObjectListAdapter)customer_list.getAdapter()).notifyDataSetChanged();
    }
}
